package com.hanhanxiaochong.zhongxiangchuanmei.view.order.act;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.LoginInfoBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.OrderGoodDetailBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.OrderProjectDetailBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.WXPayBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.OrderDetailPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.ShopMapActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IOrderDetailView;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.utils.TimeUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/2.
 * Desc   : 订单带服务act
 */

public class OrderWaitServiceActivity extends BaseActivity<OrderDetailPresenter> implements IOrderDetailView {

    @BindView(R.id.txt_left)
    TextView txtLeft;
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.ll_leftview)
    RelativeLayout llLeftview;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.ll_rightview)
    RelativeLayout llRightview;
    @BindView(R.id.view_bottomline)
    View viewBottomline;
    @BindView(R.id.rl_topview)
    RelativeLayout rlTopview;
    @BindView(R.id.txt_shopname)
    TextView txtShopname;
    @BindView(R.id.img_phone)
    ImageView imgPhone;
    @BindView(R.id.img_location)
    ImageView imgLocation;
    @BindView(R.id.iv_good_pic)
    ImageView ivGoodPic;
    @BindView(R.id.iv_self_support)
    ImageView ivSelfSupport;
    @BindView(R.id.tv_good_name)
    TextView tvGoodName;
    @BindView(R.id.tv_project_fit)
    TextView tvProjectFit;
    @BindView(R.id.tv_vip_price)
    TextView tvVipPrice;
    @BindView(R.id.tv_normal_price)
    TextView tvNormalPrice;
    @BindView(R.id.tv_project_time)
    TextView tvProjectTime;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_order_status)
    TextView tvOrderStatus;
    @BindView(R.id.tv_order_phone)
    TextView tvOrderPhone;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_order_technician)
    TextView tvOrderTechnician;
    @BindView(R.id.tv_order_service_time)
    TextView tvOrderServiceTime;
    @BindView(R.id.tv_order_deliver_way)
    TextView tvOrderDeliverWay;
    @BindView(R.id.txt_code)
    TextView txtCode;
    @BindView(R.id.txt_notice)
    TextView txtNotice;
    private String token;
    private String order_id;
    private String order_num;
    private LoginInfoBean loginInfo;
    private String lat;
    private String lng;

    public static Intent createIntent(Context context, String order_id, String order_num) {
        Intent intent = new Intent(context, OrderWaitServiceActivity.class);
        intent.putExtra("order_id", order_id);
        intent.putExtra("order_num", order_num);
        return intent;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_order_waitservice);
        loginInfo = SharedPreferencesUtil.getsInstances(this).getLoginInfo();
        token = loginInfo.getToken();
        Intent intent = getIntent();
        order_id = intent.getStringExtra("order_id");
        order_num = intent.getStringExtra("order_num");
    }

    @Override
    protected void initPresenter() {
        mPresenter = new OrderDetailPresenter(this);
    }

    @Override
    protected void init() {
        setTopView();
        mPresenter.getProjectInfo(token, order_id, order_num);
    }

    private void setTopView() {
        imgLeft.setImageResource(R.mipmap.icon_left);
        txtTitle.setText("订单详情");
    }

    @OnClick({R.id.ll_leftview, R.id.img_location, R.id.img_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.img_location:
                if (lat != null && lng != null) {
                    startActivity(ShopMapActivity.createIntent(this, Double.parseDouble(lat),
                            Double.parseDouble(lng), txtShopname.getText().toString().trim(), ""));
                }
                showTip("地图位置");
                break;
            case R.id.img_phone:
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + loginInfo.getMobile()));
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
                break;
        }
    }

    @Override
    public void setGoodInfo(OrderGoodDetailBean data) {

    }

    @Override
    public void setProjectInfo(OrderProjectDetailBean data) {
        if (data.getS_name() != null)
            txtShopname.setText(data.getS_name());
        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + data.getPic().split(",")[0], R.mipmap.img_error, R.mipmap.img_error, ivGoodPic);
        tvGoodName.setText(data.getP_name());
        ivSelfSupport.setVisibility(data.getSelf_support().equals("y") ? View.VISIBLE : View.GONE);
        tvVipPrice.setText(String.format(ResourcesUtils.getString(R.string.vip_good_price), data.getPrice()));
        tvProjectFit.setText(String.format(ResourcesUtils.getString(R.string.project_fit), data.getSymptom_lists()));
        tvProjectTime.setText(String.format(ResourcesUtils.getString(R.string.project_time), data.getWhen_long()));

        tvOrderNum.setText(String.format(ResourcesUtils.getString(R.string.order_num), data.getOrder_num()));
        tvOrderPhone.setText(String.format(ResourcesUtils.getString(R.string.order_phone), SharedPreferencesUtil.getsInstances(this).getLoginInfo().getMobile()));
        tvOrderTime.setText(String.format(ResourcesUtils.getString(R.string.order_time), TimeUtils.getDateFromSeconds(data.getAdd_time())));
        tvOrderStatus.setText(String.format(ResourcesUtils.getString(R.string.order_status), "待服务"));
        tvOrderTechnician.setText(String.format(ResourcesUtils.getString(R.string.order_status), data.getT_name()));
        tvOrderServiceTime.setText(String.format(ResourcesUtils.getString(R.string.order_status), data.getTment_time()));
        lat = data.getLat();
        lng = data.getLng();
        txtCode.setText(String.format(ResourcesUtils.getString(R.string.order_service_code), data.getOrder_num()));
    }

    @Override
    public void hasDelivered(String data) {

    }

    @Override
    public void aliPay(String data) {

    }

    @Override
    public void wxPay(WXPayBean data) {

    }
}
